package network;

import ihm.MainFrame;
import ihm.panel.BMPCapture;

import java.net.*;
import java.io.*;
import java.util.*;

import cipher.algorithm.Algorithm;
import cipher.algorithm.Empty;

//** Classe principale du serveur, gere les infos globales **/
public class KrakenServer extends Thread {
	
	private static final int DEFAULT_PORT_NUMBER = 18000;
	private ArrayList<PrintWriter> tabClients;
	private int nbClients;
	private int port;
	private boolean serverOn, sendKey;
	private ServerSocket serverSocket;
	
	private BMPCapture capture;
	private MainFrame mainFrame;
	private Algorithm algo = new Empty();
	private static int MODE = 0;
	
	//** Methode : la première methode executee, elle attend les connections **/
	public KrakenServer(MainFrame mainFrame, BMPCapture capture, String p) {
		this.capture = capture;
		this.mainFrame = mainFrame;
		try {
			tabClients = new ArrayList<PrintWriter>(); // contiendra tous les flux de sortie vers les clients
			nbClients = 0;
			serverOn = true;
			sendKey = false;
			
			if(p.equals(""))	port = DEFAULT_PORT_NUMBER; //port 18000 par défaut
			else	port = new Integer(p); //sinon premier argument

			printWelcome(port);
			serverSocket = new ServerSocket(port);
	    }
	    catch(Exception e) {
	    	e.printStackTrace();
	    }
	}
	
	@Override
	public void run() {
		try {
			while(serverOn) {
		    	new KrakenThread(serverSocket.accept(), this);
		    }
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//** Methode : affiche le message d'accueil **/
	public void printWelcome(Integer port){
		System.out.println("--------");
		System.out.println("Kr4ken");
		System.out.println("Projet Kr4ken M1 ISIM SIC 2013");
		System.out.println("Version 0.1 : 03/05/2013");
		System.out.println("--------");
		System.out.println("Demarre sur le port : " +port.toString());
		System.out.println("--------");
	}


	//** Methode : envoie le message a tous les clients **/
	synchronized public void sendAll(String message,String sLast) {
		PrintWriter out;
		
		for(int i = 0; i < tabClients.size(); i++){ 
			out = (PrintWriter) tabClients.get(i);
			
			if(out != null){
				out.println(message+sLast);
				out.flush();
			}
		}
	}

	//** Methode : detruit le client no i **/
	synchronized public void delClient(int i){
		nbClients--;
		
		System.out.println("del client");
		System.out.println(tabClients.size());
		if(tabClients.get(i) != null){
			tabClients.remove(i);
		}
	}
	
	public int getPort(){
		return this.port;
	}
	
	public void setServerOnOff() throws IOException {
		serverOn = false;
	}

	//** Methode : ajoute un nouveau client dans la liste **
	synchronized public int addClient(PrintWriter out){
		nbClients++;
		tabClients.add(out);
		return tabClients.size()-1;
	}

	//** Methode : retourne le nombre de clients connectes **/
	synchronized public int getNbClients(){
		return nbClients;
	}

	public BMPCapture getCapture() { return capture; }
	public void setCapture(BMPCapture capture) { this.capture = capture; }
	
	public MainFrame getMainFrame() { return mainFrame; }
	public void setMainFrame(MainFrame mainFrame) { this.mainFrame = mainFrame; }

	public Algorithm getAlgo() { return algo; }
	public void setAlgo(Algorithm algo) { this.algo = algo; }

	public static int getMODE() { return MODE; }
	public static void setMODE(int mODE) { MODE = mODE; }
	
	public boolean getSendKey(){	return this.sendKey;	}
	public void setSendKey(boolean send){	this.sendKey = send;	}
	
}
